<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\finance\model\Account as AccountModel;
use app\user\model\User as UserModel;
use app\shop\model\Shop as ShopModel;
use app\finance\model\BankInterturn as BankInterturnModel;
use app\system\model\systemUser as systemUserModel;
use think\Db;
use think\facade\Env;
use League\Csv\Reader;
use League\Csv\CannotInsertRecord;
use League\Csv\Writer;
class Achievements extends Admin
{
	/**
	 * [ledger 绩效明细账]
	 * @return [type] [description]
	 */
	public function ledger()
	{
             if ($this->request->isAjax()) {

                  $where      = $data = [];
                  $page       = $this->request->param('page/d', 1);
                  $limit      = $this->request->param('limit/d', 15);
                  $user_id    = $this->request->param('user_id','');
                  $admin_id    = $this->request->param('admin_id','');
                  $shop_id    = $this->request->param('shop_id','');
                  $admin_type    = $this->request->param('admin_type','');
                  $operation    = $this->request->param('operation',1);
                  $start      = $this->request->param('start', '2000-01-01');
                  $end      = $this->request->param('end',date('Y-m-d H:i:s',time()));
                  if(!is_empty($user_id)){
                        $where['user_id'] = ['=',$user_id];
                  }
                  if(!is_empty($admin_id)){
                         $where['admin_id'] = ['=',$admin_id];
                  }
                  if(!is_empty($shop_id)){
                         $where['shop_id'] = ['=',$shop_id];
                  }
                  if(!is_empty($admin_type)){
                         $where['admin_type'] = ['=',$admin_type];
                  }
                  $type = [
                  	1 => '项目',
                  	2 => '产品',
                  	3 => '会员卡',
                  	8 => '疗程卡'
                  ];
                  // halt($end);
                  if($operation == 1){
                        $data['data'] = Db::name('achievements')->where($where)
                        ->where('createtime','between',[$start,date('Y-m-d H:i:s',strtotime($end.'+1 days'))])
                        ->page($page)->limit($limit)->order('id desc')->select();
                        foreach ($data['data'] as $key => $value) {
                              $data['data'][$key]['user_name'] = Db::name('user')->where('id', $value['user_id'])->value('nick');
                              $data['data'][$key]['shop_name'] = Db::name('shop')->where('id', $value['shop_id'])->value('name');
                              $data['data'][$key]['admin_name'] = Db::name('system_user')->where('id', $value['admin_id'])->value('nick');
                              $data['data'][$key]['price1'] = '';
                              $data['data'][$key]['price2'] = '';
                              $data['data'][$key]['price3'] = '';
                              $data['data'][$key]['price4'] = '';
                              if(strpos($value['project'],'疗程卡充值') !== false){
                                    $data['data'][$key]['price1'] = $value['price'];
                              }
                              if(strpos($value['project'],'产品购买') !== false){

                                    $data['data'][$key]['price2'] = $value['price'];
                              }
							if(strpos($value['project'],'会员退产品') !== false){
																    $data['data'][$key]['price2'] = '-'.$value['price'];
															}
                              if(strpos($value['project'],'会员卡充值') !== false){
                                    $data['data'][$key]['price3'] = $value['price'];
                              }
							if(strpos($value['project'],'项目购买') !== false){
                                    $data['data'][$key]['price4'] = $value['price'];
                              }
                        }
                        $data['count']  = Db::name('achievements')->where($where)
                        ->where('createtime','between',[$start,date('Y-m-d H:i:s',strtotime($end.'+1 days'))])
                        ->count('id');
                        $data['code']   = 0;
                        return json($data);
                  }else if($operation == 2){

                        // halt(Env::get('runtime_path'));Env::get('runtime_path')
                        // $fileName =  'csv/jixiao'.DIRECTORY_SEPARATOR.time().'.csv';

                        $records = Db::name('achievements')->where($where) 
                        ->where('createtime','between',[$start,date('Y-m-d H:i:s',strtotime($end.'+1 days'))])
                        ->order('id desc')->select();
                        foreach ($records as $key => $value) {
                              $records[$key]['user_name'] = Db::name('user')->where('id', $value['user_id'])->value('nick');
                              $records[$key]['shop_name'] = Db::name('shop')->where('id', $value['shop_id'])->value('name');
                              $records[$key]['admin_name'] = Db::name('system_user')->where('id', $value['admin_id'])->value('nick');
                              $records[$key]['proportion_price'] = $value['proportion'] * $value['price'] / 100;
					$records[$key]['proportion'] = $value['proportion'] . '%';
                              unset($records[$key]['admin_id']);
                              unset($records[$key]['user_id']);
                              unset($records[$key]['shop_id']);
                              $records[$key]['price1'] = '';
                              $records[$key]['price2'] = '';
                              $records[$key]['price3'] = '';
							$records[$key]['price4'] = '';
                              if(strpos($value['project'],'疗程卡充值') !== false){
                                    $records[$key]['price1'] = $value['price'];
                              }
                              if((strpos($value['project'],'产品购买') !== false) || (strpos($value['project'],'会员退产品') !== false)){
                                    $records[$key]['price2'] = $value['price'];
                              }

                              if(strpos($value['project'],'会员卡充值') !== false){
                                    $records[$key]['price3'] = $value['price'];
                              }
								if(strpos($value['project'],'项目购买') !== false){
                                    $records[$key]['price4'] = $value['price'];
                              }
                        }
                        $data['data'] = $records;
                        $data['count']  = Db::name('achievements')->where($where)
                        ->where('createtime','between',[$start,date('Y-m-d H:i:s',strtotime($end.'+1 days'))])->count('id');
                        $data['code']   = 0;

			      return json($data);
                        // halt($records);
                        // $top = ['ID','项目名称','数量','金额','手工','结算方式','职位','提成比例','添加时间','订单编号','会员','门店','员工','提成金额'];
                        // try {
                        //       $writer = Writer::createFromPath('./'.$fileName, 'w+');
                        //       $writer->insertOne($top);
                        //       $writer->insertAll($records); //using an array
                        // } catch (CannotInsertRecord $e) {
                        //       return $this->error($e->getRecords());
                        // }
                        // return $this->request->domain() .DIRECTORY_SEPARATOR.$fileName;
                  }

              }
              // halt(UserModel::all());
            $this->assign('shops', ShopModel::getSelect(ShopModel::all()));
            $this->assign('users', userModel::getSelect(UserModel::all()));
            $this->assign('admins', systemUserModel::getSelect(systemUserModel::where('type','in',[5,7])->all()));
		// halt($projects);
		return $this->fetch();
	}

	public function setProportion()
	{
		$postData = $this->request->post();
		if(!Db::name('achievements')->where('id',$postData['id'])->update($postData)){
			return $this->axiosMsg('更新失败',false);
		}
		return $this->axiosMsg('更新成功',true);
		// halt($postData);
	}


}
